﻿using System.Linq;
using System.Web.Mvc;
using System.Web.Routing;
using System.Net;
using WebPattern.DataAccess;
using WebPattern.Core.MultiCulture;
using WebPattern.MVC.Handler;

namespace WebPattern.MVC
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        private static bool multiCultureEnabled = false;
        
        protected void Application_Start()
        {

            // Configuration

            #region NHibernate
            //HibernatingRhinos.Profiler.Appender.NHibernate.NHibernateProfiler.Initialize();
            var factory = NHibernateSessionManager.ConfigureFromFile(Server.MapPath("~/hibernate.config"));
            #endregion

            #region log4net
            // log4net.config
            System.IO.FileInfo fi = new System.IO.FileInfo(Server.MapPath("~/log4net.config"));
            if (fi != null && fi.Exists)
            {
                // Code that runs on application startup
                log4net.Config.XmlConfigurator.Configure(fi);
            }

            // web.config
            //log4net.Config.XmlConfigurator.Configure();

            // set properti hostName
            log4net.GlobalContext.Properties["hostName"] = Dns.GetHostName();
            #endregion

            // --------------- //
            
            
            multiCultureEnabled = true;

            // MVC configuration

            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);

            // --------------- //
        }

        #region Helper Methods
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new WebPatternHandlerErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.IgnoreRoute("Content/{*pathInfo}");
            routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" });
            
            //routes.IgnoreRoute("{resource}/Error/{*pathInfo}"); // - ingore route handle for error control

            if (multiCultureEnabled)
            {
                routes.MapRoute(
                                    "Default", // Route name
                                    "{culture}/{controller}/{action}/{id}", // URL with parameters
                                    new
                                    {
                                        culture = MultiCultureHelper.CultureCodeDic.Where(p => p.Key == CultureKey.Default).First().Value,
                                        controller = "Home",
                                        action = "Index",
                                        id = UrlParameter.Optional
                                    }  
                                ).RouteHandler = new MultiCultureMvcRouteHandler(); 
            }
            else
            {
                routes.MapRoute(
                                "Default", // Route name
                                "{controller}/{action}/{id}", // URL with parameters
                                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
                                );
            }

        }
        #endregion
    }
} 